<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title id="title">聊天室</title>
</head>
<body>
<div class="talk_con">
</div>
</body>
<script type="text/javascript">
    var ws = new WebSocket("wss://ks-gz-live-comet-02.chat.bilibili.com/sub");
    var interId = 0;
    //连接发生错误的回调方法
    ws.onerror = function(event){
        console.log("onerror:" + event);
    };
    //连接成功建立的回调方法
    ws.onopen = function(event){
        console.log("onopen:" + event);
        // 心跳
        interId = setInterval(function() {
            //ws.send('{"type":"2"}');
            ws.send('ping');
        }, 30000);
    }
    //接收到消息的回调方法
    ws.onmessage = function(event){
        console.log("onmessage:" + event.data);
    }
    //连接关闭的回调方法
    ws.onclose = function(event){
        console.log("onclose:" + event.code + ": " + event.reason);
        clearInterval(interId);
    }

</script>
</html>